//
//  ReadingViewController.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ReadingViewController.h"

@interface ReadingViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)NSMutableArray *scrollArray;
@property (nonatomic,strong)UISwipeGestureRecognizer *swipe;

@end

@implementation ReadingViewController


-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
-(NSMutableArray *)scrollArray{
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        //设置item大小
        layout.itemSize = CGSizeMake(100, 100);
        layout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3, kWidth, kHeight/2+10) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        
    }
    return _collectionView;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"reading";
    //注册XIB
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"read_cell"];
    [self.view addSubview:self.collectionView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [RequestManager fetchDataWithUrl:kReadUrl parameter:@{} method:@"GET" completed:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"%@",dic);
        
        //添加轮播图的数据源
        for (NSDictionary *smallDic in dic[@"data"][@"carousel"]) {
            [self.scrollArray addObject:smallDic[@"img"]];
        }
        //添加collectionView的数据源
        for (NSDictionary *dictionary in dic[@"data"][@"list"]) {
            ReadModel *model = [[ReadModel alloc]init];
            [model setValuesForKeysWithDictionary:dictionary];
            [self.dataSource addObject:model];
        }
        
        
        
    } updateUI:^{
        //创建轮播图
        ScrollView *scroll = [[ScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3) imageArray:self.scrollArray];
        [self.view addSubview:scroll];

        [self.collectionView reloadData];
    }];
    
    
    //添加手势
    self.swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipeAction:)];
    self.swipe.direction = UISwipeGestureRecognizerDirectionRight;
    
    [self.view addGestureRecognizer:self.swipe];
    [self.view becomeFirstResponder];
    
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipeAction:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipe];
    
    
    
    
}
-(void)rightSwipeAction:(UISwipeGestureRecognizer *)sender{
    [UIView animateWithDuration:0.1 animations:^{
        self.navigationController.view.frame = CGRectMake(kWidth/2, 0, kWidth, kHeight+64);
        self.isSlider = YES;
    }];

}


-(void)leftSwipeAction:(UISwipeGestureRecognizer *)sender{
    [UIView animateWithDuration:0.1 animations:^{
        self.navigationController.view.frame = CGRectMake(0, 0, kWidth, kHeight+64);
        self.isSlider = YES;
    }];
    

}


#pragma -----collectionViewDelegate-----
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"read_cell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor blueColor];
    //设置cell
    ReadModel *model = self.dataSource[indexPath.row];
    [cell setCellWithModel:model];
    return cell;
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //通过XIB获取控制器
    ReadListViewController *readList = [[ReadListViewController alloc]initWithNibName:@"ReadListViewController" bundle:nil];
    ReadModel *model  = self.dataSource[indexPath.row];
    readList.typeID = model.type;
    
    //只能获取view
//    ReadListViewController *readList = [[NSBundle mainBundle]loadNibNamed:@"ReadListViewController" owner:nil options:nil].firstObject;
    [self.navigationController pushViewController:readList animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
